package com.zn.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author YinJiaWei
 * @version 2018/9/3 16:51
 */
public class GZIPUtils {
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";


    public static byte[] compress(String str, String encoding) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static byte[] compress(String str) throws IOException {
        return compress(str, GZIP_ENCODE_UTF_8);
    }

    public static byte[] uncompress(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static String uncompressToString(byte[] bytes, String encoding) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString(encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String uncompressToString(byte[] bytes) {
        return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
    }

    public static void main(String[] args) throws IOException {
        String s="{\n" +
                "  \"lowestPrice\": \"8330.000\",\n" +
                "  \"askVolume1\": \"10\",\n" +
                "  \"openInterest\": \"136026.000\",\n" +
                "  \"fifteenMinVolume\": \"7726\",\n" +
                "  \"openPrice\": \"8512.000\",\n" +
                "  \"preSettlementPrice\": \"8488.000\",\n" +
                "  \"bidVolume1\": \"281\",\n" +
                "  \"actionDay\": \"20180831\",\n" +
                "  \"tradingDay\": \"20180831\",\n" +
                "  \"preOpenInterest\": \"145144.000\",\n" +
                "  \"fiveMinVolume\": \"4398\",\n" +
                "  \"upperLimitPrice\": \"8998.000\",\n" +
                "  \"bidPrice1\": \"8380.000\",\n" +
                "  \"turnover\": \"1347805568.000\",\n" +
                "  \"highestPrice\": \"8546.000\",\n" +
                "  \"lowerLimitPrice\": \"7978.000\",\n" +
                "  \"instrumentID\": \"SM901\",\n" +
                "  \"oneHrVolume\": \"26200\",\n" +
                "  \"askPrice1\": \"8382.000\",\n" +
                "  \"updateTime\": \"14:59:59\",\n" +
                "  \"fourHrVolume\": \"35308\",\n" +
                "  \"thirtyMinVolume\": \"13572\",\n" +
                "  \"volume\": \"160148\",\n" +
                "  \"preClosePrice\": \"8508.000\",\n" +
                "  \"instrumentName\": \"锰硅901\",\n" +
                "  \"oneMinVolume\": \"1394\",\n" +
                "  \"dayVolume\": \"85790\",\n" +
                "  \"settlementPrice\": \"8416\",\n" +
                "  \"averagePrice\": \"8416.000\",\n" +
                "  \"lastPrice\": \"8380.000\"\n" +
                "}";
        System.out.println("字符串长度："+s.length());
        System.out.println("压缩后：："+compress(s));
        System.out.println("解压后："+uncompress(compress(s)).length);
        System.out.println("解压字符串后：："+uncompressToString(compress(s)));
    }
}
